package com.qiaoba.module.system.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.system.entity.SysConfig;
import com.qiaoba.module.system.entity.dto.SysConfigDTO;
import com.qiaoba.module.system.entity.param.SysConfigParam;
import com.qiaoba.module.system.service.SysConfigService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 参数配置 Web层
 *
 * @author ailanyin
 * @date 2023-09-09 22:09:33
 */
@Validated
@RestController
@RequestMapping("/system/config")
@RequiredArgsConstructor
@Tag(name = "参数配置")
public class SysConfigController {

    private final SysConfigService sysConfigService;

    @PreAuthorize("hasAuthority('system:config:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public PageResult<SysConfig> list(@Validated @ParameterObject SysConfigParam param, @ParameterObject PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysConfig> list = sysConfigService.selectList(param);
        return PageResult.build(list);
    }

    @PreAuthorize("hasAuthority('system:config:query')")
    @GetMapping(value = "/{configId}")
    @Operation(summary = "获取详情")
    public Result<SysConfig> getInfo(@Size(min = 19, max = 20, message = "ID不合法") @PathVariable String configId) {
        return Result.success(sysConfigService.selectById(configId, false));
    }

    @GetMapping(value = "/configKey/{configKey}")
    @Operation(summary = "根据参数键名查询参数值")
    public Result getConfigKey(@PathVariable String configKey) {
        return Result.success(sysConfigService.selectConfigByKey(configKey));
    }

    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('system:config:add')")
    @PostMapping
    @Operation(summary = "新增配置")
    public Result add(@Validated(AddGroup.class) @RequestBody SysConfigDTO dto) {
        return Result.toAjax(sysConfigService.insert(dto));
    }

    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:config:edit')")
    @PutMapping
    @Operation(summary = "修改配置")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysConfigDTO dto) {
        return Result.toAjax(sysConfigService.updateById(dto));
    }

    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:config:remove')")
    @DeleteMapping("/{configIds}")
    @Operation(summary = "删除配置")
    public Result remove(@PathVariable List<String> configIds) {
        return Result.toAjax(sysConfigService.deleteByIds(configIds));
    }

    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:config:remove')")
    @DeleteMapping("/refreshCache")
    @Operation(summary = "刷新缓存")
    public Result refreshCache() {
        sysConfigService.resetCache();
        return Result.success();
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("hasAuthority('system:config:export')")
    @PostMapping("/export")
    @Operation(summary = "导出数据")
    public void export(@Validated HttpServletResponse response, SysConfigParam param) {
        List<SysConfig> list = sysConfigService.selectList(param);
        ExcelUtil.exportExcel(list, SysConfig.class, "参数数据", response);
    }
}
